//
//  MXSMainTabBarVC.swift
//  weibo
//
//  Created by 孙飞 on 15/12/5.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import SVProgressHUD

class MXSMainTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.darkGrayColor()
        
        
        // 不能直接对一个只读属性赋值
//        self.tabBar = UITabBar()
        
        let tabBar = MXSTabBar()
        
        tabBar.composeButtonClosure = { [weak self] in
            
            if !MXSUserAccountVM.sharedAccount.userLogon {
                SVProgressHUD.showErrorWithStatus("请先登录")
                return
            }
            
            // 初始化 composeView
            let composeView = MXSEditView()
            composeView.show(self)
        }
        
        setValue(tabBar, forKeyPath: "tabBar")
        
        
        let homeVC = MXSHomeVC()
        homeVC.title = "首页"
        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
        homeVC.tabBarItem.selectedImage = UIImage(named: "tabbar_home_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        homeVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        addChildViewController(MXSNavController(rootViewController: homeVC))
        
        
        addChildViewController(MXSMessageVC(), imgName: "tabbar_message_center", title: "消息")
        addChildViewController(MXSDiscoverVC(), imgName: "tabbar_discover", title: "发现")
        addChildViewController(MXSProfileVC(), imgName: "tabbar_profile", title: "我")

        
    }
    
    // 添加子控制器方法
    func addChildViewController(childController: UIViewController, imgName: String, title: String) {
        
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: imgName)
        // 渲染图片
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        // title color
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        addChildViewController(MXSNavController(rootViewController: childController))
    }
    
    deinit {
        print("delack!!!!!!")
    }

    
    

}
